package com.wanban

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import android.widget.Toast
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants
import com.wanban.adutils.utils.*
import com.wanban.base.BaseActivity

class MainActivity : BaseActivity() {

    private val pauseAd = CommonAdTools()
    private val bannerAd = CommonAdTools()
    private val rewardedAdUtils = CommonAdTools()
    
    private lateinit var interstitialButton: Button
    private lateinit var rewardVideoButton: Button
    private lateinit var btnBanner: Button
    private lateinit var btnNative: Button
    private lateinit var pbNative: ProgressBar
    private lateinit var pbBanner: ProgressBar
    private lateinit var pbReward: ProgressBar
    private lateinit var pbInterstitial: ProgressBar
    private lateinit var adNativeContainer: ViewGroup
    private lateinit var adBannerContainer: ViewGroup

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (OpenAdTools.isAdLoadSuccess()) {
            Log.e(AdConstants.TAG, "进入首页，查询当前开屏已经加载完成，准备加载广告...")
            OpenAdTools.showAd(this)
        }

        ReopenAdTools.init(this)
        // 预加载详情广告
//        if (!VideoInterstitialAdTools.isAdLoadSuccess()) {
//            VideoInterstitialAdTools.loadAd(this)
//        }

        interstitialButton = findViewById(R.id.interstitialButton)
        rewardVideoButton = findViewById(R.id.rewardVideoButton)
        btnBanner = findViewById(R.id.btnBanner)
        btnNative = findViewById(R.id.btnNative)
        pbNative = findViewById(R.id.pbNative)
        pbReward = findViewById(R.id.pbReward)
        pbBanner = findViewById(R.id.pbBanner)
        pbInterstitial = findViewById(R.id.pbInterstitial)
        adNativeContainer = findViewById(R.id.adNativeContainer)
        adBannerContainer = findViewById(R.id.adBannerContainer)

        interstitialButton.setOnClickListener {
            Log.e("wbAd", "加载插屏广告...")
            loadInterstitialAd()
        }

        rewardVideoButton.setOnClickListener {
            loadRewardedAd()
        }

        btnBanner.setOnClickListener {
            loadAndShowBanner()
        }

        btnNative.setOnClickListener {
            loadAndShowNative()
        }
    }

    private fun loadAndShowNative() {
        pbNative.visibility = View.VISIBLE
        if (adNativeContainer.childCount >= 2) {
            adNativeContainer.removeViewAt(1)
        }
        pauseAd.loadAd(this, AdConstants.AD_ID_VIDEO_DETAIL_PAUSE, object : OnLoadAdCallback() {

            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                Log.e("ISAds", "NativeAdTools..onAdLoaded...")
                pbNative.visibility = View.GONE
                pauseAd.showAd(adNativeContainer)
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                pbNative.visibility = View.GONE
                Toast.makeText(this@MainActivity, "中矩形Banner广告加载失败", Toast.LENGTH_SHORT).show()
                Log.e("ISAds", "NativeAdTools..onAdLoadFailed...")
            }

            override fun onAdComplete() {
                super.onAdComplete()
                Log.e("ISAds", "NativeAdTools..onAdComplete...")
                if (adNativeContainer.childCount >= 2) {
                    adNativeContainer.removeViewAt(1)
                }

            }
        })
    }

    //
////
////    private fun setNativeButtonStat(isEnable: Boolean) {
////        if (isEnable) {
////            btnNative.text = "Show Native Ad"
////        } else {
////            btnNative.text = "Native Ad Loading..."
////        }
////    }
////
////
////
//
    private fun loadAndShowBanner() {
        pbBanner.visibility = View.VISIBLE
        if (adBannerContainer.childCount >= 2) {
            adBannerContainer.removeViewAt(1)
        }
        bannerAd.loadAd(this, AdConstants.AD_ID_VIDEO_DETAIL_BANNER, object : OnLoadAdCallback() {

            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                Log.e("ISAds", "BannerAdTools..onAdLoaded...")
                pbBanner.visibility = View.GONE
                bannerAd.showAd(adBannerContainer)
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                Log.e("ISAds", "BannerAdTools..onAdLoadFailed...")
                pbBanner.visibility = View.GONE
            }

            override fun onAdComplete() {
                super.onAdComplete()
                Log.e("ISAds", "NativeAdTools..onAdComplete...")
                if (adBannerContainer.childCount >= 2) {
                    adBannerContainer.removeViewAt(1)
                }
            }
        })
    }


    private fun loadRewardedAd() {
        pbReward.visibility = View.VISIBLE
        rewardedAdUtils.loadAd(this, AdConstants.AD_ID_LOCK_REWARD_AD, object : OnLoadAdCallback() {

            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                Log.e("ISAds", "RewardLockAdTools..onAdLoaded...")
                pbReward.visibility = View.GONE
                rewardedAdUtils.showAdInActivity(this@MainActivity)
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                Log.e("ISAds", "RewardLockAdTools..onAdLoadFailed...$errorCode")
                pbReward.visibility = View.GONE
                Toast.makeText(this@MainActivity, "激励广告加载失败...", Toast.LENGTH_SHORT).show()
            }

            override fun onAdComplete() {
                super.onAdComplete()
                Log.e("ISAds", "InterstitialVideoAdTools..onAdComplete...")
            }
        })
    }
//
//
//
//    fun showInterstitial(view: View) {
//        if (interstitialAd.isAdLoadSuccess()) {
//            interstitialAd.showAd(adBannerContainer)
//        } else {
//            setInterstitialButtonStat(false)
//            loadInterstitialAd()
//        }
//    }
//
//

    private fun loadInterstitialAd() {
        pbInterstitial.visibility = View.VISIBLE
        if (VideoInterstitialAdTools.isAdLoadSuccess()) {
            Log.e("wbAd", "插屏广告预加载成功...")
            pbInterstitial.visibility = View.GONE
        }

        VideoInterstitialAdTools.loadAd(this, object : OnLoadAdCallback() {
            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                Log.e("ISAds", "InterstitialVideoAdTools..onAdLoaded...")
                pbInterstitial.visibility = View.GONE
                VideoInterstitialAdTools.showAd(this@MainActivity)
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                pbInterstitial.visibility = View.GONE
                Toast.makeText(this@MainActivity, "插屏广告加载失败...", Toast.LENGTH_SHORT).show()
                Log.e("ISAds", "InterstitialVideoAdTools..onAdLoadFailed...")
            }

            override fun onAdComplete() {
                super.onAdComplete()
                Log.e("ISAds", "InterstitialVideoAdTools..onAdComplete...")
            }
        })
    }


}